// 任务通知相关类型定义

export interface TaskNotification {
  id: number // 后端是Long类型
  userId: number // 后端是Long类型
  taskId?: string
  taskType?: string
  type: 'TASK' | 'NOTIFICATION' // 任务类型：TASK-任务，NOTIFICATION-通知
  status: 'PENDING' | 'RUNNING' | 'COMPLETED' | 'FAILED' | 'UNREAD' | 'READ' // 任务状态或通知状态
  title: string
  content: string
  result?: string // 任务结果，JSON格式
  progress?: number // 任务进度，0-100
  isRead: boolean // 是否已读
  createTime: string // 后端是LocalDateTime，前端接收为字符串
  updateTime?: string // 后端是LocalDateTime，前端接收为字符串
}

// 后端返回的IPage<TaskNotification>结构
export interface TaskNotificationPageResponse {
  code: number
  message: string
  data: {
    records: TaskNotification[]
    total: number
    size: number
    current: number
    pages: number
  }
}

// 保持向后兼容
export interface TaskNotificationListResponse {
  code: number
  message: string
  data: {
    list: TaskNotification[]
    total: number
    page: number
    size: number
  }
}

export interface TaskNotificationCountResponse {
  code: number
  message: string
  data: number // 后端直接返回数字，不是对象
}

export interface TaskNotificationResponse {
  code: number
  message: string
  data: TaskNotification
}

export interface CommonResponse {
  code: number
  message: string
}

// 便于前端使用的枚举
export const TaskNotificationType = {
  TASK: 'TASK',
  NOTIFICATION: 'NOTIFICATION'
} as const

export const TaskNotificationStatus = {
  // 任务状态
  PENDING: 'PENDING',
  RUNNING: 'RUNNING',
  COMPLETED: 'COMPLETED',
  FAILED: 'FAILED',
  // 通知状态
  UNREAD: 'UNREAD',
  READ: 'READ'
} as const

// 将后端状态转换为前端显示文本
export const getStatusText = (status: string): string => {
  switch (status) {
    case TaskNotificationStatus.PENDING:
      return '等待中'
    case TaskNotificationStatus.RUNNING:
      return '进行中'
    case TaskNotificationStatus.COMPLETED:
      return '已完成'
    case TaskNotificationStatus.FAILED:
      return '失败'
    case TaskNotificationStatus.UNREAD:
      return '未读'
    case TaskNotificationStatus.READ:
      return '已读'
    default:
      return '未知'
  }
}

// 将后端类型转换为前端显示文本
export const getTypeText = (type: string): string => {
  switch (type) {
    case TaskNotificationType.TASK:
      return '任务'
    case TaskNotificationType.NOTIFICATION:
      return '通知'
    default:
      return '未知'
  }
}

// 判断是否为任务状态
export const isTaskStatus = (status: string): boolean => {
  return status === TaskNotificationStatus.PENDING || 
         status === TaskNotificationStatus.RUNNING || 
         status === TaskNotificationStatus.COMPLETED || 
         status === TaskNotificationStatus.FAILED
}

// 判断是否为通知状态
export const isNotificationStatus = (status: string): boolean => {
  return status === TaskNotificationStatus.UNREAD || 
         status === TaskNotificationStatus.READ
}

// 判断是否为未读状态
export const isUnread = (item: TaskNotification): boolean => {
  if (item.type === TaskNotificationType.TASK) {
    return item.status !== TaskNotificationStatus.COMPLETED && item.status !== TaskNotificationStatus.FAILED
  } else {
    return !item.isRead
  }
}